/**
 * @desc 教练
 */
<template>
    <view class="coach">
        <nut-swipe v-for="(item, index) in coach" :key="index" :style="{ margin: '10px 0' }">
            <template #left v-if="item.stop">
                <nut-button shape="square" style="height:100%" type="info" @click="pass(index)">通过</nut-button>
            </template>
            <nut-input v-if="!item.nickName" :label="`编号${index + 1}：`" placeholder="请输入" v-model="item.coach" />
            <nut-cell v-else :title="item.nickName">
                <template v-slot:link>
                    <nut-avatar size="small" :icon="item.avatarUrl" :style="{ marginRight: '5px' }"></nut-avatar>
                    <nut-tag v-if="item.stop" type="danger">未通过</nut-tag>
                </template>
            </nut-cell>
            <template #right>
                <nut-button shape="square" style="height:100%" type="danger" @click="deleteId(index)">删除</nut-button>
                <nut-button shape="square" style="height:100%" type="info" @click="add(index)">新增</nut-button>
            </template>
        </nut-swipe>
        <view class="coach-btn">
            <nut-button v-if="!coach.length" type="primary" @click="add(-1)">新增</nut-button>
            <nut-button v-else type="primary" @click="save">保存</nut-button>
        </view>
        <nut-dialog title="提示" content="是否保存当前信息!" v-model:visible="visible" @cancel="onCancel" @ok="onOk" />
    </view>
</template>

<script>

import { reactive, toRefs, } from 'vue';
import db from '../../server/index';
import Taro from '@tarojs/taro';
const timeArr = [
    {
        startTime: '07:00:00',
        endTime: '09:00:00',
        limit: 3
    },
    {
        startTime: '09:00:00',
        endTime: '11:30:00',
        limit: 3
    },
    {
        startTime: '17:00:00',
        endTime: '19:30:00',
        limit: 3
    }
]
export default {
    name: 'Coach',
    data() {
        return {
            coach: [],
            index: -1,
            visible: false,
            deleteList: []
        }
    },
    created() {
        this.init()
    },
    onPullDownRefresh() {
        Taro.showLoading({
            title: '加载中',
        })
        this.init()
    },
    methods: {
        deleteId(index) {
            if (this.coach[index]._id) {
                // 从库中删除
                this.deleteList.push(this.coach[index]._id)
            }
            this.coach = this.coach.filter((a, b) => b !== index)
        },
        add(index) {
            this.index = index + 1
            this.coach.splice(index + 1, 0, { coach: '', stop: false })
        },
        save() {
            this.visible = true
        },
        onCancel() {
            this.visible = false
        },
        onOk() {
            this.deleteList.forEach(item => {
                db.collection('coach').where({
                    _id: item
                }).remove();
            })
            this.deleteList = []
            this.coach.forEach(a => {
                if (a._id) {
                    db.collection('coach').where({
                        _id: a._id
                    }).update({
                        data: {
                            coach: a.coach,
                            stop: a.stop
                        }
                    }).then(() => {
                        !a.stop && db.collection('time').where({
                            coachId: a._id
                        }).get().then(({ data = [] }) => {
                            if (!data.length) {
                                timeArr.forEach((item, index) => {
                                    db.collection('time').add({
                                        data: {
                                            ...item,
                                            coachId: a._id,
                                            index
                                        }
                                    })
                                })
                            }
                        })
                        Taro.showToast({
                            title: '修改成功!',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                        })
                    })
                } else {
                    db.collection('coach').add({
                        data: {
                            coach: a.coach,
                            stop: a.stop
                        }
                    }).then(() => {
                        // 初始化 三条时间数据
                        timeArr.forEach((item, index) => {
                            db.collection('time').add({
                                data: {
                                    ...item,
                                    coachId: a._id,
                                    index
                                }
                            })
                        })
                        Taro.showToast({
                            title: '保存成功!',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                        })
                    })
                }
            })
        },
        pass(index) {
            this.coach[index].stop = false
        },
        init() {
            db.collection('coach').get().then(({ data = [] }) => {
                this.coach = data.sort((a, b) => a.index < b.index ? -1 : 1)
                Taro.stopPullDownRefresh();
                Taro.hideLoading();
            })
        }
    },
}
</script>

<style lang="less">
.coach {
    width: 100vw;

    &-btn {
        text-align: center;
        margin-top: 10px;
    }
}
</style>
